package com.hh.service.service.impl.integral;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.common.model.integral.IntegralOrderInfo;
import com.hh.common.vo.integral.IntegralOrderInfoDetailVo;
import com.hh.common.vo.integral.StoreIntegralOrderInfoVo;
import com.hh.service.dao.integral.IntegralOrderInfoDao;
import com.hh.service.service.integral.IntegralOrderInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class IntegralOrderInfoServiceImpl extends ServiceImpl<IntegralOrderInfoDao, IntegralOrderInfo> implements IntegralOrderInfoService {

    @Resource
    private IntegralOrderInfoDao dao;

    @Override
    public boolean create(IntegralOrderInfo integralOrderInfo) {
        return dao.insert(integralOrderInfo) > 0;
    }

    @Override
    public List<IntegralOrderInfo> getListByOrderNo(String orderId) {
        LambdaQueryWrapper<IntegralOrderInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(IntegralOrderInfo::getOrderNo, orderId);
        return dao.selectList(lqw);
    }

    @Override
    public HashMap<Integer, List<StoreIntegralOrderInfoVo>> getMapInId(List<Integer> orderList) {
        HashMap<Integer, List<StoreIntegralOrderInfoVo>> map = new HashMap<>();
        if(orderList.size() < 1){
            return map;
        }
        LambdaQueryWrapper<IntegralOrderInfo> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(IntegralOrderInfo::getOrderId, orderList);
        List<IntegralOrderInfo> systemStoreStaffList = dao.selectList(lambdaQueryWrapper);
        if(systemStoreStaffList.size() < 1){
            return map;
        }
        for (IntegralOrderInfo storeOrderInfo : systemStoreStaffList) {
            //解析商品详情JSON
            StoreIntegralOrderInfoVo StoreOrderInfoVo = new StoreIntegralOrderInfoVo();
            BeanUtils.copyProperties(storeOrderInfo, StoreOrderInfoVo, "info");
            IntegralOrderInfoDetailVo integralOrderInfoDetailVo = new IntegralOrderInfoDetailVo();
            BeanUtil.copyProperties(storeOrderInfo,integralOrderInfoDetailVo);
            //StoreOrderInfoVo.setInfo(JSON.parseObject(storeOrderInfo.getInfo(), IntegralOrderInfoDetailVo.class));
            StoreOrderInfoVo.setInfo(integralOrderInfoDetailVo);
            if(map.containsKey(storeOrderInfo.getOrderId())){
                map.get(storeOrderInfo.getOrderId()).add(StoreOrderInfoVo);
            }else{
                List<StoreIntegralOrderInfoVo> storeOrderInfoVoList = new ArrayList<>();
                storeOrderInfoVoList.add(StoreOrderInfoVo);
                map.put(storeOrderInfo.getOrderId(), storeOrderInfoVoList);
            }
        }
        return map;
    }

    @Override
    public List<StoreIntegralOrderInfoVo> getOrderListByOrderId(Integer orderId) {
        LambdaQueryWrapper<IntegralOrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(IntegralOrderInfo::getOrderId, orderId);
        List<IntegralOrderInfo> systemStoreStaffList = dao.selectList(lambdaQueryWrapper);
        if(systemStoreStaffList.size() < 1){
            return null;
        }

        List<StoreIntegralOrderInfoVo> storeOrderInfoVoList = new ArrayList<>();
        for (IntegralOrderInfo storeOrderInfo : systemStoreStaffList) {
            //解析商品详情JSON
            StoreIntegralOrderInfoVo storeOrderInfoVo = new StoreIntegralOrderInfoVo();
            BeanUtils.copyProperties(storeOrderInfo, storeOrderInfoVo, "info");
            IntegralOrderInfoDetailVo integralOrderInfoDetailVo = new IntegralOrderInfoDetailVo();
            BeanUtil.copyProperties(storeOrderInfo,integralOrderInfoDetailVo);
            storeOrderInfoVo.setInfo(integralOrderInfoDetailVo);
            //storeOrderInfoVo.setInfo(JSON.parseObject(storeOrderInfo.getInfo(), OrderInfoDetailVo.class));
            storeOrderInfoVo.getInfo().setIsReply(0);
            storeOrderInfoVoList.add(storeOrderInfoVo);
        }
        return storeOrderInfoVoList;
    }
}
